package com.newtribe.access;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.net.SocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.SocketChannel;

/**
 * 
 */

/**
 * @author newtribe
 *
 */
public class NioSocketAgent {

	/**
	 * @param args
	 */
	public static void main(String[] args)throws Exception {
		
		SocketAddress address = new InetSocketAddress("studio",9000);
		SocketChannel client=SocketChannel.open(address);
		client.configureBlocking(false);
		String a="httpk";
		ByteBuffer buffer=ByteBuffer.allocate(20);
		buffer.put(a.getBytes());
		buffer.clear();
		int d=client.write(buffer);
		
		System.out.println("发送数据: "+new String(buffer.array()));
		while(true)
		{
			buffer.flip();
			int i=client.read(buffer);
			if(i>0){
			byte[] b=buffer.array();
			System.out.println("接收数据: "+new String(b));
			client.close();
			System.out.println("连接关闭!");
			break;
		}
		
		
		
		// TODO Auto-generated method stub
		  Socket  serverSocket =new Socket("studio",9000);
		  
		  System.out.println("connect fuwei:9000"); 
		  serverSocket.setKeepAlive(true);
	    DataInputStream in =new DataInputStream(serverSocket.getInputStream()) ;
	    DataOutputStream out  =new DataOutputStream(serverSocket.getOutputStream() );
//	    out.write(1);
//	    out.flush();
//	    
		  Socket  rdpSocket =new Socket("192.168.11.130",3389);
		    DataInputStream rdpIn =new DataInputStream(rdpSocket.getInputStream()) ;
		    DataOutputStream rdpOut  =new DataOutputStream(rdpSocket.getOutputStream() );
		    
		    try {
				StreamUtil.forwardPacket(in, out, rdpIn, rdpOut);
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		    
		    
		System.out.println("xxxxxxxxxxxxxxxxxxxx socket agent exit ");


 		  
	
	}
	

}
}
